import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';

import {Backend} from '../../providers/backend';
import {Forget} from './tab-my-forget';

import {Registration} from './tab-my-registration';
import { Events } from 'ionic-angular';

@Component({
  templateUrl: 'build/pages/tab-my/tab-my-login.html'
})
export class LoginPage {
  static get parameters() {
    return [[NavController], [NavParams], [Backend], [Events]];
  }
  constructor(nav, params, backend, events) {
    this.nav = nav;
    this.params = params;
    this.backend = backend;
    this.events = events;
    this.wrongmessage;
    this.wrongmessage1;
    this.wrongmessage2;
    this.password;
    this.email;
    this.suvery = {};
  }
  forgetpassword() {
    this.nav.push(Forget);
  }
  // testEmail() {
  //   var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  //   if (!filter.test(this.email)) {
  //     this.wrongmessage1 = this.wrongmessage = '*请输入正确邮箱地址';
  //   } else {
  //     this.wrongmessage = undefined;
  //     this.wrongmessage1 = undefined;
  //   }
  // }
  // testpaw() {
  //   var paw = /^(?=.{6,16}$)(?![0-9]+$)(?!.*(.).*\1)[0-9a-zA-Z]+$/;
  //   if (!paw.test(this.password)) {
  //     this.wrongmessage2 = this.wrongmessage = '*请输入6-16位不完全相同的数字和字母';
  //   } else {
  //     this.wrongmessage = undefined;
  //     this.wrongmessage2 = undefined;
  //   }
  // }
  // login() {
  //   if (typeof (this.email) === 'string' && typeof (this.password) === 'string' && this.wrongmessage1 === undefined && this.wrongmessage2 === undefined) {
  //     console.log('tiao');
  //     this.nav.push(TabMy, { id: 1 });
  //   } else if (this.wrongmessage === undefined && (typeof (this.email) === 'undefined' || typeof (this.password) === 'undefined')) {
  //     this.wrongmessage = '*邮箱或密码不能为空';
  //   } else if (typeof (this.wrongmessage1) === 'string') {
  //     this.wrongmessage = '*请输入正确邮箱地址';
  //   } else if (typeof (this.wrongmessage2) === 'string') {
  //     this.wrongmessage = '*请输入6-16位不完全相同的数字和字母';
  //   }
  // }
  registration() {
    this.nav.push(Registration);
  }

  goBack() {
    this.nav.pop().then(() => {
      this.params.data.resolve(false);
    });
  }

  // login() {

  //   this.backend.login(this.email, this.password)
  //     .then((result) => {
  //       console.log(result);
  //       if (result.status == 0) {
  //         return this.nav.pop();
  //       } else {
  //         throw new Error('用户名密码错误.');
  //       }
  //     }).then(() => {
  //       this.params.data.resolve(true);
  //     }).catch((error) => {
  //       console.log(error);
  //     });
  // }
  login() {
    this.backend.login(this.email, this.password)
      .then(result => {
        this.wrongmessage=result.info;
        if (result.code == '1') {
          this.suvery = result.result[0].userid;
          console.log(this.suvery);
          this.nav.pop();
          this.events.publish('user:created', result.result[0].username);
        }
       
      });

  }
}
